﻿using GENERIC.MVVM.Model;
using GENERIC.Service.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace GENERIC.ViewModel.Commad
{
    /// <summary>
    /// Classe de la commande Delete
    /// </summary>
    /// <typeparam name="TEntityType"></typeparam>
    public class DeleteCommand<TEntityType> : ICommand where TEntityType : ModelBase
    {
        #region Events

        /// <summary>
        /// Evénement CanExecuteChanged
        /// </summary>
        public event EventHandler CanExecuteChanged;

        #endregion

        #region Constructor

        /// <summary>
        /// Initialise une nouvelle instance
        /// </summary>
        /// <param name="service">Service</param>
        /// <param name="canExecute">Fonction canExecute</param>
        /// <param name="deleted">Fonction delete</param>
        /// <param name="withConfirmation">Avec confirmation de l'utilisateur ou non</param>
        public DeleteCommand(IService<TEntityType> service, Func<bool> canExecute, Action<TEntityType> deleted, bool withConfirmation = true)
        {
            Service = service;
            CanExecuteF = canExecute;
            Deleted = deleted;
            WithConfirmation = withConfirmation;
        }

        #endregion

        #region Properties

        private IService<TEntityType> Service { get; set; }

        private Func<bool> CanExecuteF { get; set; }

        private Action<TEntityType> Deleted { get; set; }

        private bool WithConfirmation { get; set; }

        #endregion

        #region Methods

        /// <summary>
        /// Obtenir une réponse si la commande peut être exécutée ou non
        /// </summary>
        /// <param name="parameter">Pramètre de la commande</param>
        /// <returns>Résultat</returns>
        public bool CanExecute(object parameter)
        {
            return CanExecuteF();
        }

        /// <summary>
        /// Exécution de la commande
        /// </summary>
        /// <param name="parameter">Paramètre de la commande</param>
        public void Execute(object parameter)
        {
            if (CanExecute(parameter))
            {
                var entity = parameter as TEntityType;
                if (entity != null)
                {
                    var result = true;
                    if (WithConfirmation)  throw new NotImplementedException("Are you sure you wish to delete the contact?");
                    if (result)
                    {
                        Service.Delete(entity);
                        if (Deleted != null)
                        {
                            Deleted(entity);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Déclencher l'événement CanExecuteChanged
        /// </summary>
        public void RaiseCanExecuteChanged()
        {
            var handler = CanExecuteChanged;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        #endregion
    }
}
